package com.ruoyi.generator.service.impl;

import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.generator.domain.GenTableColumnFetcher;
import com.ruoyi.generator.domain.GenTableColumnTable;
import com.ruoyi.generator.domain.dto.GenTableColumnDbView;
import com.ruoyi.generator.service.IGenTableColumnService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.sql.JSqlClient;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务字段 服务层实现
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class GenTableColumnServiceImpl implements IGenTableColumnService {

    private final JSqlClient sqlClient;
    private final JdbcTemplate jdbcTemplate;

    @Override
    public List<GenTableColumnDbView> selectDbTableColumnsByName(String tableName) {
        String dataSql = "select column_name,\n" +
                "\t\t       (case when (is_nullable = 'no' and column_key != 'PRI') then '1' else '0' end) as is_required,\n" +
                "\t\t       (case when column_key = 'PRI' then '1' else '0' end) as is_pk,\n" +
                "\t\t       ordinal_position as sort,\n" +
                "\t\t       column_comment,\n" +
                "\t\t       (case when extra = 'auto_increment' then '1' else '0' end) as is_increment,\n" +
                "\t\t       column_type\n" +
                "\t\tfrom information_schema.columns where table_schema = (select database()) and table_name = ('" + tableName + "')\n" +
                "\t\torder by ordinal_position";
        return jdbcTemplate.query(dataSql, (rs, rowNum) -> {
            GenTableColumnDbView view = new GenTableColumnDbView();
            view.setIsRequired(rs.getBoolean("is_required"));
            view.setIsPk(rs.getBoolean("is_pk"));
            view.setSort(rs.getInt("sort"));
            view.setColumnComment(rs.getString("column_comment"));
            view.setColumnName(rs.getString("column_name"));
            view.setIsIncrement(rs.getBoolean("is_increment"));
            view.setColumnType(rs.getString("column_type"));
            return view;
        });
    }

    /**
     * 查询业务字段列表
     *
     * @param tableId 业务字段编号
     * @return 业务字段集合
     */
    @Override
    public List<GenTableColumn> selectGenTableColumnListByTableId(String tableId) {
        GenTableColumnTable table = GenTableColumnTable.$;
        return sqlClient.createQuery(table)
                .where(table.tableId().eq(tableId))
                .orderBy(table.sort())
                .select(table.fetch(
                        GenTableColumnFetcher.$
                                .allScalarFields()
                                .tableId()
                ))
                .execute();
    }

    /**
     * 新增业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    /*@Override
    public int insertGenTableColumn(GenTableColumn genTableColumn) {
        return genTableColumnMapper.insertGenTableColumn(genTableColumn);
    }*/

    /**
     * 修改业务字段
     *
     * @param genTableColumn 业务字段信息
     * @return 结果
     */
    /*@Override
    public int updateGenTableColumn(GenTableColumn genTableColumn) {
        return genTableColumnMapper.updateGenTableColumn(genTableColumn);
    }*/

    /**
     * 删除业务字段对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    /*@Override
    public int deleteGenTableColumnByIds(String ids) {
        return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
    }*/
}
